/*
 * Copyright 2010-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.kamismile.akka.core;


import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import com.gitee.kamismile.akka.di.SpringExtension;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ClassUtils;

import static org.springframework.util.Assert.notNull;


@Deprecated
public class StoneAkkaFactoryBean  implements FactoryBean<ActorRef>, ApplicationContextAware {

  private Class<BaseActor> mapperInterface;

  private ApplicationContext applicationContext;

  public StoneAkkaFactoryBean() {
    // intentionally empty
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
  }

  public StoneAkkaFactoryBean(Class<BaseActor> mapperInterface) {
    this.mapperInterface = mapperInterface;
  }



  /**
   * {@inheritDoc}
   */
  @Override
  public ActorRef getObject() throws Exception {
      // left intentionally blank
      ActorSystem actorSystem = this.applicationContext.getBean(ActorSystem.class);
      SpringExtension springExtension = this.applicationContext.getBean(SpringExtension.class);
      String shortClassName= ClassUtils.getShortNameAsProperty(mapperInterface)+"AkkaRef";
      ActorRef actorRef = actorSystem.actorOf(
              springExtension.props(shortClassName), shortClassName + "RealAkka");
      return actorRef;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Class<ActorRef> getObjectType() {
    return ActorRef.class;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isSingleton() {
    return false;
  }

  // ------------- mutators --------------


  /**
   * Return the mapper interface of the MyBatis mapper
   *
   * @return class of the interface
   */
  public Class<BaseActor> getMapperInterface() {
    return mapperInterface;
  }

}
